package pwd.allen;

import java.io.*;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 测试下cpu密集、io密集时 查看负载
 *
 * @author 门那粒沙
 * @create 2020-03-21 22:51
 **/
public class LoadMain {
    public static void main(String[] args) throws InterruptedException, IOException {
        String action = "cpu";
        Integer sleep = 0;
        if (args != null) {
            if (args.length >= 1) {
                action = args[0];
            }
            if (args.length >= 2) {
                sleep = Integer.valueOf(args[1]);
            }
        }
        while (true) {
            if (sleep > 0) {
                TimeUnit.MILLISECONDS.sleep(sleep);
            }
            if ("cpu".equals(action)) {
                System.out.println(new Random().nextInt(Integer.MAX_VALUE));
            } else if ("io".equals(action)) {
                String str = "hello world!";
                File file = new File(System.getProperty("user.home") + File.separator + "test.txt");
                if (!file.exists()) {
                    file.createNewFile();
                    FileOutputStream outputStream = new FileOutputStream(file);
                    for (int i = 0; i < 1000; i++) {
                        outputStream.write(str.getBytes());
                    }
                    outputStream.close();
                }
                BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
                System.out.println(bufferedReader.lines());
                bufferedReader.close();
            }
        }
    }
}
